/*
* Copyright (c) 2008, Russell Garrett
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.moneydance.modules.features.portfolioallocation;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

import com.moneydance.apps.md.controller.FeatureModuleContext;
import com.moneydance.apps.md.model.RootAccount;

/**
 * Wrapper for storing global settings in the root account. Handles custom
 * serialization of types.
 */

public class Settings {
	private static Settings me = null;

	public static Settings instance() {
		if (me == null) {
			me = new Settings();
		}
		return me;
	}

	private FeatureModuleContext fmc;
	private String prefix;

	public void initialize(FeatureModuleContext fmc, String prefix) {
		this.fmc = fmc;
		this.prefix = prefix;
	}

	private RootAccount getRoot() {
		return fmc.getRootAccount();
	}

	public void save(String key, String value) {
		System.out.println("Save: " + value);
		getRoot().setParameter(prefix + "." + key, value);
	}

	public void saveMapStringString(String key, Map<String, String> value) {
		StringBuilder result = new StringBuilder();
		try {
			for (Map.Entry<String, String> entry : value.entrySet()) {
				result.append(entry.getKey());
				result.append("=");
				result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
				result.append(",");
			}
		} catch (Exception e) {

		}
		save(key, result.toString());
	}

	public String load(String key, String def) {
		return getRoot().getParameter(prefix + "." + key, def);
	}

	public String load(String key) {
		return load(key, null);
	}

	public Map<String, String> loadMapStringString(String key,
			Map<String, String> map) {
		String value = load(key);
		if (value == null)
			return map;
		String[] items = value.split(",");
		for (String item : items) {
			String[] kv = item.split("=");
			if (kv.length != 2)
				continue;
			try {
				map.put(kv[0], URLDecoder.decode(kv[1], "UTF-8"));
			} catch (Exception e) {
			}
		}
		return map;
	}

}
